

 file open fh using .\Results\Table_Discrim.tex, write replace 

 file write fh "{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
	"\begin{tabular}{l*{9}{c}}" _n ///
	"\toprule \toprule" _n ///
	"& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\" _n ///
	"  & \multicolumn{2}{c}{Men discriminated} & \multicolumn{2}{c}{Women discriminated} & \multicolumn{2}{c}{White discriminated} & \multicolumn{2}{c}{BME discriminated} \\ "_n ///
	"\cline{2-9}" _n
	
	local model discrimmodelQ
	local col = 8

* White*Male
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local var WhiteMale
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "White Male & `b'`star'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "& `b'`star'" 
}
if `i'==`col' {	
	est restore `model'`i'
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "& `b'`star' \\" _n
	}
	}

* Second write p-values
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh " & (`pval')" 
}
if `i' >1 & `i'<`col' {
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh "& (`pval')" 
}
if `i' ==`col' {
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh "& (`pval') " _n
}
	}	
	
	
	
	* The commands below complete the document
file write fh  "\\ \midrule" _n  ///
				"Sample &          All       &   All  & All & All &          All       &   All  & All & All  \\[0.4ex]" _n 
* Adding mean
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "Mean dep variable      & `mean'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "& `mean'" 
}
if `i'==`col' {
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "& `mean' \\[0.4ex] " _n
}
	}
				
file write fh	"Individual controls  & &  $\checkmark$    &  &  $\checkmark$ & &  $\checkmark$    &  &  $\checkmark$  \\[0.4ex]" _n
* Adding observations
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "N.obs      & `N'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "& `N'" 
}
if `i'==`col' {
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "& `N' \\ \bottomrule \bottomrule " _n
}
	}
	
				
file write fh		"\end{tabular}}" _n 
file close fh

